abstract class AbstractBuffer {
    protected _gl: WebGLRenderingContext;

    constructor(gl: WebGLRenderingContext) {
        this._gl = gl;
    }

    protected _initArrayBuffer(buffer: WebGLBuffer | null, data: number[]) {
        this._gl.bindBuffer(this._gl.ARRAY_BUFFER, buffer);
        this._gl.bufferData(this._gl.ARRAY_BUFFER, new Float32Array(data), this._gl.STATIC_DRAW);

        this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
    }

    protected _initIndexBuffer(buffer: WebGLBuffer | null, indices: number[]) {
        this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, buffer);
        const uint16 = new Uint16Array(indices);
        this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, uint16, this._gl.STATIC_DRAW);
        this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, null);
    }

    protected _vertexAttribPointer(location: number, buffer: WebGLBuffer | null, size: number) {
        this._gl.bindBuffer(this._gl.ARRAY_BUFFER, buffer);
        const FSIZE = Float32Array.BYTES_PER_ELEMENT;
        this._gl.vertexAttribPointer(location, size, this._gl.FLOAT, false, FSIZE * size, 0);
        this._gl.enableVertexAttribArray(location);
        this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
    }
}

export default AbstractBuffer;
